﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Data;

namespace Foxy.GridViewControls
{
    internal class CheckBoxItem : CheckBoxField
    {
        public const string CheckBoxID = "CheckBoxButton";
        /// <summary>
        /// Internal storage for the readOnly flag.
        /// </summary>
        private bool readOnly = true;
        /// <summary>
        /// Used to set the DataField that we wish to represent with this CheckBox.
        /// </summary>
        //public string DataField { get; set; }
        
        /// <summary>
        /// Set the AutoPostBack flag. If this is true then each time a CheckBox is clicked 
        /// in the Column that contains this item then an event is raised on the server.
        /// </summary>
        public bool AutoPostBack { set; get; }

        /// <summary>
        /// Our CheckChanged event
        /// </summary>
        public event EventHandler CheckedChanged;

        protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
        {
            base.InitializeDataCell(cell, rowState);

            // Add a checkbox anyway, if not done already
            if (cell.Controls.Count == 0)
            {
                CheckBox chk = new CheckBox();
                chk.ID = CheckBoxItem.CheckBoxID;
                //chk.DataBinding += new EventHandler(this.BindData);
                chk.CheckedChanged += new EventHandler(this.OnCheckChanged);
                cell.Controls.Add(chk);
            }
        }

        /// <summary>
        /// This is a common handler for all the Checkboxes.
        /// </summary>
        /// <param name="sender">The raiser of this event a CheckBox.</param>
        /// <param name="e">A System.EventArgs that contains the event data.</param>
        private void OnCheckChanged(object sender, EventArgs e)
        {
            if (CheckedChanged != null)
            {
                CheckedChanged(sender, e);
            }
        }


        /// <summary>
        /// Handler for the DataBinding event where we bind the data for a specific row 
        /// to the CheckBox.
        /// </summary>
        /// <param name="sender">The raiser of the event.</param>
        /// <param name="e">A System.EventArgs that contains the event data.</param>
        //private void BindData(object sender, EventArgs e)
        //{
        //    CheckBox box = (CheckBox)sender;
        //    GridViewRow container = (GridViewRow)box.NamingContainer;
        //    box.Checked = false;
        //    //box.Enabled = (readOnly == true) ? false : true;
        //    string data = ((DataRowView)container.DataItem)[this.DataField].ToString();
        //    Type t = ((DataRowView)container.DataItem).DataView.Table.Columns[this.DataField].DataType;
        //    if (data.Length > 0)
        //    {
        //        switch (t.ToString())
        //        {
        //            case "System.Boolean":
        //                if ((data == "True") || (data == "true"))
        //                {
        //                    box.Checked = true;
        //                }
        //                break;
        //            default:
        //                break;
        //        }
        //    }
        //}
    }
}
